package com.guqj.nio.netty.serialization.marshalling;

import io.netty.handler.codec.marshalling.DefaultMarshallerProvider;
import io.netty.handler.codec.marshalling.DefaultUnmarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallingDecoder;
import io.netty.handler.codec.marshalling.MarshallingEncoder;
import io.netty.handler.codec.marshalling.UnmarshallerProvider;

import java.io.IOException;

import org.jboss.marshalling.Marshaller;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;
import org.jboss.marshalling.Unmarshaller;

public final class MarshallingCodeCFactory {

	private final static String SERIAL = "serial";
	private final static int VERSION = 5;
	
	/*
	 * 创建 JBOSS Marshalling解码器MarshallingDecoder MarshallingDecoder将二进制数组解码为对象
	 * 
	 * @return MarshallingDecoder
	 */
	public static MarshallingDecoder buildMarshallingDecoder() {

		/*
		 * 通过 Marshalling 工具类的 getProvidedMarshallerFactory
		 * 静态方法获取MarshallerFactory 实例, , 参数 serial 表示创建的是 Java 序列化工厂对象.它是由
		 * jboss-marshalling-serial 包提供
		 */
		final MarshallerFactory MarshallerFactory = Marshalling.getProvidedMarshallerFactory(SERIAL);
		UnmarshallerProvider provider = new DefaultUnmarshallerProvider(MarshallerFactory, buildMarshallingConfiguration());
		/*
		 * provider : 提供商 maxSize : 单个对象最大尺寸
		 */
		int maxSize = 1024 << 2;
		return new MarshallingDecoder(provider, maxSize);
	}

	/*
	 * 创建 JBOSS Marshalling编码器MarshallingEncoder,MarshallingDecoder将实现序列化接口的POJO对象序列化为二进制数组
	 * 
	 * @return MarshallingDecoder
	 */
	public static MarshallingEncoder buildMarshallingEncoder() {
		return new MarshallingEncoder(buildMarshallerProvider());
	}
	
	
	public static MarshallerProvider buildMarshallerProvider(){
		/*
		 * 通过 Marshalling 工具类的 getProvidedMarshallerFactory
		 * 静态方法获取MarshallerFactory 实例, , 参数 serial 表示创建的是 Java 序列化工厂对象.它是由
		 * jboss-marshalling-serial 包提供
		 */
		final MarshallerFactory MarshallerFactory = Marshalling.getProvidedMarshallerFactory(SERIAL);
		MarshallerProvider provider = new DefaultMarshallerProvider(MarshallerFactory, buildMarshallingConfiguration());
		return provider;
	}
	
	/**
	 * 创建Jboss Marshaller
	 * @throws IOException
	 */
	public static Marshaller buildMarshalling() throws IOException {
		final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory(SERIAL);
		Marshaller marshaller = marshallerFactory.createMarshaller(buildMarshallingConfiguration());
		return marshaller;
	}

	/**
	 * 创建Jboss Unmarshaller
	 * @throws IOException
	 */
	public static Unmarshaller buildUnMarshalling() throws IOException {
		final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory(SERIAL);
		final Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(buildMarshallingConfiguration());
		return unmarshaller;
	}
	
	/**
	 * 创建Jboss MarshallingConfiguration
	 * @return
	 */
	private static MarshallingConfiguration buildMarshallingConfiguration() {
		final MarshallingConfiguration configuration = new MarshallingConfiguration();
		configuration.setVersion(VERSION);
		return configuration;
	}
}
